﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.BLL;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.Forum;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.Forum
{
    public partial class BBSReplyCK : DBPageBase
    {

        #region 属性

        /// <summary>
        /// 帖ID
        /// </summary>
        public string ForumID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ForumID"]))
                    return Request.QueryString["ForumID"];
                return string.Empty;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public int PageIndex
        {
            get
            {
                try
                {

                    return int.Parse(Request.QueryString["PageIndex"]);
                }
                catch (Exception ex)
                {
                    return 0;
                }
               
            }
        }
        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// 回复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnReply_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ForumID))
            {
                if (string.IsNullOrEmpty(txtDescription.Text))
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "kk", "<script>alert('回复内容不能为空');</script>");
                    return;
                }
                if (CheckDescriptionWordText())
                {
                    return;
                }
                var forum = DB.PartyForums.Find(ForumID);
                if (forum == null)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "kk",
                        "<script>alert('数据异常，请稍后再试');</script>");
                    return;
                }
                PartyForumReply reply = new PartyForumReply();
                reply.ForumID = ForumID;
                reply.ID = StringHelper.NewID;
                reply.ReplyContent = txtDescription.Text;
                reply.ReplyTime = DateTime.Now;
                reply.ReplyBy = CurrentUser.ID;
                reply.Source = 1;
                DB.PartyForumReplys.Add(reply);
                forum.ReplyTime = reply.ReplyTime;
                forum.ReplyBy = CurrentUser.LoginID;
                forum.ReplyByName = CurrentUser.UserName;
                forum.ReplyCount += 1;
                forum.TodayReplyTimes += 1;
                DB.SaveChanges();
                PartyScoreHelper.Add(() => PartyScoreHelper.AddForum(SysEnum.ForumScoreType.Reply, CurrentUser.LoginID, CurrentUser.LoginID, forum.ID, reply.ID));
                this.ClientScript.RegisterStartupScript(this.GetType(), "kk",
                    "<script>alert('回复成功！');UrlRefresh(\"BBSReplyDetails.aspx?ForumID=" +
                    ForumID + "&PageIndex=" + PageIndex + "\")</script>");
                PartyForum.RunForumTypeStatistics();
                PartyForum.RunForumUserInfoStatistics(CurrentUser.LoginID);
            }
            else
            {

            }
        }

        private bool CheckDescriptionWordText()
        {
            var list = DB.KeyWords.Where(a=> txtDescription.Text.Contains(a.WordText)).ToList();
            if (list.Count > 0)
            {
                string strKW = "";
                list.ForEach(a =>
                {
                    string str = a.WordText;
                    strKW += "[" + str + "]";
                    this.txtDescription.Text = this.txtDescription.Text.Replace(str,
                        string.Format(@"<span style='background-color: rgb(255, 0, 0);'>{0}</span>", str));

                });
                this.ClientScript.RegisterStartupScript(this.GetType(), "kk",
                    @"<script>alert('内容含有以下敏感词：\t\n" + strKW + "请马上修改！');</script>");
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}